﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WRJClient.DAL;

namespace WRJClient.UI
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public string TitleName { get; set; } = "无人机巡检系统";
        public LoginWindow()
        {
            InitializeComponent();
            DataContext = this;
            ip.Text = Properties.Settings.Default.ServerIp;
            port.Text = Properties.Settings.Default.ServerPort.ToString();
        }

        private void showLogin()
        {
            LoginPanel.Visibility = Visibility.Visible;
            SettingPanel.Visibility = Visibility.Collapsed;
        }
        private void showSetting()
        {
            LoginPanel.Visibility = Visibility.Collapsed;
            SettingPanel.Visibility = Visibility.Visible;
        }

        private void saveSetting()
        {
            Properties.Settings.Default.ServerIp = ip.Text.ToString();
            Properties.Settings.Default.ServerPort = int.Parse(port.Text.ToString());
            Properties.Settings.Default.Save();
        }

        private void login()
        {
            if (Api.Auth(userBox.Text, passwordBox.Password))
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show("登录失败！");
            }
            Close();
        }

        private void cancel()
        {
            Close();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                if (button.Name.CompareTo("SettingBtn") == 0)
                {
                    showSetting();
                }
                else if (button.Name.CompareTo("SettingSaveBtn") == 0)
                {
                    saveSetting();
                    showLogin();
                }
                else if (button.Name.CompareTo("SettingCancelBtn") == 0)
                {
                    showLogin();
                }
                else if (button.Name.CompareTo("LoginBtn") == 0)
                {
                    login();
                }
                else if (button.Name.CompareTo("CancelBtn") == 0)
                {
                    cancel();
                }
            }
        }
    }
}
